classdef AbstractDetector < handle
    %ABSTRACTDETECTOR �˴���ʾ�йش����ժҪ
    %   �˴���ʾ��ϸ˵��
    
    properties
        name
        type
        setting
        
        port
    end
    
    methods
        function obj = AbstractDetector(name, type, varargin)
            %ABSTRACTDETECTOR ��������ʵ��
            %   �˴���ʾ��ϸ˵��
            p=inputParser;
            p.addRequired('name', @ischar);
            p.addRequired('type', @(x) ismember(x, {'PD', 'BPD'}));
            p.addParameter('filename', [Constant.AutoMeasurementAppPath strrep('\+Table1\Detectors.xml', '\', Constant.PathDelimiter)], @ischar);
            p.parse(name, type, varargin{:});

            obj.name = name;
            obj.type = type;
            obj.loadDetector(p.Results.filename);
        end
        
        function loadDetector(obj, filename)
            L = log4m.getLogger;
            
            info = xml_read(filename);            
            switch obj.type
                case 'PD'
                    if ismember(obj.name, fields(info.PD))
                        param.model = info.PD.(obj.name).model;
                        param.sn = info.PD.(obj.name).sn;
                        L.info('AbstractDetector::loadDetector', sprintf('PD [%s] infomation loaded.', obj.name));
                    else
                        error('No inforamtion [type = Coils, name = %s] found.', obj.name);
                    end
                case 'BPD'
                    if ismember(obj.name, fields(info.BPD))
                        param.model = info.BPD.(obj.name).model;
                        param.sn = info.BPD.(obj.name).sn;
                        L.info('AbstractDetector::loadDetector', sprintf('BPD [%s] infomation loaded.', obj.name));
                    else
                        error('No inforamtion [type = AOMs, name = %s] found.', obj.name);
                    end
                otherwise
                    error('Unknown type [%s]', obj.type);
            end
            obj.setting = param;
        end
        
        function disp(obj)
            fprintf('%s [%s] is connected to port [%s]. \n\n', obj.type, obj.name, obj.port.name);
            disp(obj.port);
        end
        %% configuration
        function obj = connect_to(obj, port)
            L = log4m.getLogger;
            p=inputParser;
            p.addRequired('port', @(x) isa(x, 'SignalChain.IOPort.InputPort') || isa(x, 'SignalChain.IOPort.BidirectionPort'));
            p.parse(port);
            obj.port = port;
            L.info('AbstractDetector::connect_to', sprintf('%s [%s] connect to port [%s].', obj.type, obj.name, obj.port.name));
        end
        
        %%
        function obj = reset(obj)
            L = log4m.getLogger;
            obj.port.initialize();
            L.info('AbstractDetector::reset', sprintf('%s [%s] resetting port [%s]...', obj.type, obj.name, obj.port.name));
        end
    end
end

